package cn.itcast.controller;

import cn.itcast.domain.Goods;
import cn.itcast.domain.Msg;
import cn.itcast.domain.Person;
import cn.itcast.service.IPersonService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mysql.cj.x.protobuf.MysqlxDatatypes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Controller
public class PersonController {

    @Autowired
    private IPersonService personService;

    @RequestMapping("/donator")
    public String findAllDonators(Model model,@RequestParam(value="pn",required = false,defaultValue = "1")Integer pn){
        //开启分页查询，获取第pn页，每页5条数据
        PageHelper.startPage(pn,6);
        // 查询所有捐赠者
        List<Person> donators = personService.findAllDonators();
        // 将捐赠者信息存入model，传给前端
        model.addAttribute("donators",donators);
        //获取分页信息
        PageInfo<Person> pageInfo = new PageInfo<>(donators,3);
        //分页信息转为JSON传给前端
        model.addAttribute("pageInfo", JSONObject.toJSON(pageInfo));
        // 跳转到donator.jsp页面
        return "donator";
    }

    @RequestMapping("/findDonatorById/")
    @ResponseBody
    public Msg findDonatorById(@RequestParam("id") String id){
        //调用service层方法
        Person donator = personService.findDonatorById(id);
        //返回donator信息
        return Msg.success().add("donator",donator);
    }

    @RequestMapping(value = "/updateDonator", method = RequestMethod.POST)
    @ResponseBody
    public Msg updateDonator(Person donator){
        // 调用Service层方法
        personService.updateDonator(donator);
        // 返回成功信息
        return Msg.success();
    }

    @RequestMapping(value = "/deleteDonator", method = RequestMethod.POST)
    public String deleteDonatorById(@RequestParam("id") String id){
        // 调用Service层方法z
        personService.deleteDonatorById(id);
        // 重定向到donator请求
        return "redirect:donator";
    }

    @RequestMapping("/reciever")
    public String findAllReciever(Model model,@RequestParam(value="pn",required = false,defaultValue = "1")Integer pn){
        //开启分页查询，获取第pn页，每页5条数据
        PageHelper.startPage(pn,6);
        // 查询所有接收者
        List<Person> recievers = personService.findAllReciever();
        // 将接收者信息存入model，传给前端
        model.addAttribute("recievers",recievers);
        //获取分页信息
        PageInfo<Person> pageInfo = new PageInfo<>(recievers,3);
        //分页信息转为JSON传给前端
        model.addAttribute("pageInfo", JSONObject.toJSON(pageInfo));
        // 跳转到reciever.jsp页面
        return "reciever";
    }

    @RequestMapping("/findRecieverById/")
    @ResponseBody
    public Msg findRecieverById(@RequestParam("id") String id){
        //调用service层方法
        Person reciever = personService.findRecieverById(id);
        //返回reciever信息
        return Msg.success().add("reciever",reciever);
    }

    @RequestMapping(value = "/updateReciever", method = RequestMethod.POST)
    @ResponseBody
    public Msg updateReciever(Person reciever){
        // 调用Service层方法
        personService.updateReciever(reciever);
        // 返回成功信息
        return Msg.success();
    }

    @RequestMapping(value = "/deleteReciever", method = RequestMethod.POST)
    public String deleteRecieverById(@RequestParam("id") String id){
        // 调用Service层方法z
        personService.deleteRecieverById(id);
        // 重定向到reciever请求
        return "redirect:reciever";
    }
}